package com.goods.business.service.imp;

import com.goods.business.mapper.*;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class InStockServiceImpl implements InStockService {

    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductStockMapper productStockMapper;
    @Autowired
    private SupplierMapper supplierVOMapper;
    @Autowired
    private InStockInfoMapper inStockInfoMapper;

    @Override
    public List<InStockVO> findInStockList(Long id,Integer status, Integer type, String inNum, Date startTime, Date endTime) {
        return inStockMapper.selectExt(null,status,type,inNum,startTime,endTime);
    }

    @Override
    public InStockDetailVO detail(Long id) {
        InStockDetailVO inStockDetailVO = new InStockDetailVO();
        List<InStockVO> inStockVOS = inStockMapper.selectExt(id, null, null, null, null, null);
        InStockVO inStockVO = inStockVOS.get(0);
        inStockDetailVO.setInNum(inStockVO.getInNum());
        inStockDetailVO.setStatus(inStockVO.getStatus());
        inStockDetailVO.setOperator(inStockVO.getOperator());
        inStockDetailVO.setType(inStockVO.getType());
        inStockDetailVO.setTotal(inStockVO.getProductNumber());
        SupplierVO supplierVO = inStockMapper.selectSupplierVO(inStockVO.getSupplierId());
        inStockDetailVO.setSupplierVO(supplierVO);
        List<InStockItemVO> itemVOS = productMapper.selectByInNum(inStockVO.getInNum());
        inStockDetailVO.setItemVOS(itemVOS);
        return inStockDetailVO;
    }

    @Override
    public void remove(Long id) {
        updateStatus(id,1);
    }

    @Override
    public void delete(Long id) {
        inStockMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void back(Long id) {
        updateStatus(id,0);
    }

    @Override
    public void publish(Long id) {
        updateStatus(id,0);
    }

    @Override
    public void addIntoStock(InStockVO inStockVO) {
        inStockVO.setCreateTime(new Date());
        inStockVO.setModified(new Date());
        //待审核
        inStockVO.setStatus(2);
        //1: biz_in_stock  InStock对象
        InStock inStock = new InStock();
        inStock.setOperator(inStockVO.getOperator());
        inStock.setCreateTime(inStockVO.getCreateTime());
        inStock.setModified(inStockVO.getModified());
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        inStock.setInNum(uuid);
        inStock.setType(inStockVO.getType());
        inStock.setStatus(inStockVO.getStatus());
        inStock.setSupplierId(inStockVO.getSupplierId());
        List<ObjectVO> products = inStockVO.getProducts();
        int count = 0;
        for (ObjectVO product : products) {
            count = count + product.getProductNumber();
            //3: 写入 info 数据
            Product pro = productMapper.selectByPrimaryKey(product.getProductId());
            InStockInfo inStockInfo = new InStockInfo();
            inStockInfo.setInNum(inStock.getInNum());
            inStockInfo.setPNum(pro.getPNum());
            inStockInfo.setProductNumber(product.getProductNumber());
            inStockInfo.setCreateTime(inStockVO.getCreateTime());
            inStockInfo.setModifiedTime(inStockVO.getModified());
            inStockInfoMapper.insert(inStockInfo);
            //4: 更新库存
            ProductStock productStock = productStockMapper.selectByPNum(pro.getPNum());
            productStock.setStock(productStock.getStock() + product.getProductNumber());
            productStockMapper.updateByPrimaryKey(productStock);
        }
        inStock.setProductNumber(count);
        inStock.setRemark(inStockVO.getRemark());
        inStockMapper.insert(inStock);
        if(inStockVO.getSupplierId() == null){
            //2：biz_supplier  Supplier对象
            Supplier supplier = new Supplier();
            supplier.setName(inStockVO.getName());
            supplier.setAddress(inStockVO.getAddress());
            supplier.setEmail(inStockVO.getEmail());
            supplier.setPhone(inStockVO.getPhone());
            supplier.setSort(inStockVO.getSort());
            supplier.setContact(inStockVO.getContact());
            supplier.setCreateTime(inStockVO.getCreateTime());
            supplier.setModifiedTime(inStockVO.getModified());
            supplierVOMapper.insert(supplier);
        }
    }

    //修改状态码
    //0：已通过  1：回收站  2：未审核
    private void updateStatus(Long id,Integer code){
        inStockMapper.updateStatus(id,code);
    }

}
